/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-04-11
*/
package com.rzico.order.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.order.entity.WarehouseStock;
import com.rzico.order.model.WarehouseStockVo;
import com.rzico.order.service.WarehouseStockService;
import com.rzico.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 库存表控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-04-11
 */
@Api(description = "库存表接口")
@RestController
@RequestMapping("/admin/warehouseStock")
public class WarehouseStockAdminController extends BaseController {

    @Autowired
    private WarehouseStockService warehouseStockService;

    @Autowired
    private EnterpriseService enterpriseService;
    /**
     * 分页查询库存表
     *
     * @return
    */
    @ApiOperation("分页查询库存表")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query")
    })
    public CommResult<WarehouseStock> list(String startDate, String endDate, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        if (StringUtils.isNotEmpty(startDate)){
            params.put("startDate", startDate);
        }
        if (StringUtils.isNotEmpty(endDate)){
            params.put("endDate", endDate);
        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<WarehouseStock> list = warehouseStockService.selectList(params);
        PageResult<WarehouseStock> pageResult = new PageResult<WarehouseStock>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }


    /**
     * 查询最新库存
     *
     * @return
     */
    @ApiOperation("查询最新库存")
    @GetMapping("/listLastStock")
    @ApiImplicitParams({@ApiImplicitParam(name = "productName", value = "商品名称", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "brandId", value = "品牌", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "productCategoryId", value = "商品分类", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "shopId", value = "门店", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "productId", value = "商品ID", dataType = "Integer", paramType = "query"),
    })
    public CommResult<WarehouseStockVo> listLastStock(String productName, Integer brandId,Integer productCategoryId,
                                                    Integer shopId,Integer productId, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);

        Enterprise enterprise = enterpriseService.getCurrent();
        params.put("enterpriseId", enterprise.getId());
        if (StringUtils.isNotEmpty(productName)){
            params.put("productName", productName);
        }
        if (null != brandId){
            params.put("brandId", brandId);
        }
        if (null != productCategoryId){
            params.put("productCategoryId", productCategoryId);
        }
        if (null != shopId){
            params.put("shopId", shopId);
        }
        if (null != productId){
            params.put("productId", productId);
        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<WarehouseStockVo> list = warehouseStockService.selectLastStockList(params);
        PageResult<WarehouseStockVo> pageResult = new PageResult<WarehouseStockVo>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }


    /**
     * 查询单条库存表
     *
     * @return
    */
    @ApiOperation("查询单条库存表")
    @GetMapping("/find/{id}")
    public CommResult<WarehouseStock> find(@PathVariable String id) {
        WarehouseStock result = warehouseStockService.findById(id);

        Map<String,Object> data = new HashMap<>();
        data.put("warehouseStock",result);
        return CommResult.success(data);

    }

    /**
     * 保存库存表
     *
     * @param warehouseStock
     * @return
     */
    @ApiOperation("保存库存表")
    @PostMapping("/save")
    public CommResult<WarehouseStock> save(@RequestBody WarehouseStock warehouseStock) {
        int affectCount = warehouseStockService.insert(warehouseStock);
        if (affectCount <= 0) {
            return CommResult.error();
        }
        WarehouseStock result = warehouseStockService.findById(warehouseStock.getId());
        return CommResult.success(result);

    }


    /**
     * 批量删除库存表
     *
     * @param ids
     * @return
     */
    @Log(desc = "批量删除库存表", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除库存表,ids用逗号拼接")
    @PostMapping("/del/{ids}")
    public CommResult<WarehouseStock> del(@PathVariable String ids) {


        try {
            warehouseStockService.deleteByIds(ids.split(","));
        } catch (Exception e) {
            return CommResult.error(e.getMessage());
        }
        return CommResult.success();
    }

    /**
     * 更新库存表
     *
     * @param warehouseStock
     * @return
     */
    @Log(desc = "更新库存表", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("更新库存表")
    @PostMapping("/update")
    public CommResult<WarehouseStock> update(@RequestBody WarehouseStock warehouseStock) {
        int affectCount = warehouseStockService.updateByPrimaryKeySelective(warehouseStock);
        if (affectCount <= 0){
            return CommResult.error();
        }
        WarehouseStock result = warehouseStockService.findById(warehouseStock.getId());
        return CommResult.success(result);
    }

}
